#include "ui_slidebox.h"

using namespace ui;

UISlideBox::UISlideBox(UISlideBox::SLIDE_DIR SlideDir, 
										ui::UIWidget* pUIMainBox, 
										int MaxValue, 
										int CurrentValue,  
										float SingleDist,
										const math::Vec2& TopLeftCorner,
										const math::Vec2& RightBottomCorner
										) : m_SlideDir(SlideDir),
															m_pUIMainBox(pUIMainBox),
															m_MaxValue(MaxValue),
															m_CurrentValue(CurrentValue),
															m_SingleDist(SingleDist),
															m_IsMouseDown(false),
															m_IsSliding(false),
															m_CurrentSlideValue(0),
															m_TopLeft(TopLeftCorner),
															m_RightBottom(RightBottomCorner),
															m_ChangeCallBack(NULL),
															m_TimeSinceMouseDown(0)
{

	float Target = m_SingleDist * m_CurrentValue;

	const math::Vec2 TempPos = m_pUIMainBox->GetPosition();
	if(m_SlideDir == SLIDE_DIR_HOR)
		m_pUIMainBox->SetPosition( math::Vec2( -Target , TempPos.y ));
	else
		m_pUIMainBox->SetPosition( math::Vec2( TempPos.x , -Target));

}

void	UISlideBox::Tick(float dt)
{
	if(!m_pUIMainBox)
		return;

	math::Vec2 Pos;
	Pos = m_pUIMainBox->GetPosition();

	if(m_IsMouseDown)
	{
		m_TimeSinceMouseDown += dt;
	}

	if(m_IsSliding)
	{
		
		if(m_SlideDir == SLIDE_DIR_HOR)
		{	
			Pos.x -=  ( m_SingleDist * dt * m_CurrentSlideValue * 2.0f);

			if( (m_CurrentSlideValue > 0 && Pos.x <= m_ToSlidePos.x) || 
				(m_CurrentSlideValue < 0 && Pos.x >= m_ToSlidePos.x)
				)
			{
				Pos.x = m_ToSlidePos.x;	
				m_IsSliding = false;
				m_CurrentValue += m_CurrentSlideValue;
				m_CurrentSlideValue = 0;
				
				if(m_ChangeCallBack)
					m_ChangeCallBack(ui::UIWidget::WE_SLIDER_CHANGE ,m_pUIMainBox);

			}


		}	
		else if(m_SlideDir == SLIDE_DIR_VER)
		{
			Pos.y -=  ( m_SingleDist * dt * m_CurrentSlideValue * 2.0f);

			if( (m_CurrentSlideValue > 0 && Pos.y <= m_ToSlidePos.y) || 
				(m_CurrentSlideValue < 0 && Pos.y >= m_ToSlidePos.y)
				)
			{
				Pos.y = m_ToSlidePos.y;	
				m_IsSliding = false;
				m_CurrentValue += m_CurrentSlideValue;
				m_CurrentSlideValue = 0;
				
				if(m_ChangeCallBack)
					m_ChangeCallBack(ui::UIWidget::WE_SLIDER_CHANGE ,m_pUIMainBox);

			}
		}

		m_pUIMainBox->SetPosition(Pos);
	}	
	
			
}

void	UISlideBox::MouseButtonReleased(const math::Vec2& Pos)
{
	if(!m_pUIMainBox)
		return;

	const float MINIMUM_DRAG = 0.05f;
	if(m_IsSliding)
		return;

	if(!m_IsMouseDown)
		return;

	m_IsMouseDown = false;
	float Diff = 0;

	if(m_SlideDir == SLIDE_DIR_HOR)
	{
		Diff = Pos.x - m_MouseDownStartPos.x;
	}
	else
	{
		Diff = Pos.y - m_MouseDownStartPos.y;
	}

	if( m_TimeSinceMouseDown <= 0.5f )
	{
		// single move
		if(Diff > 0  && Diff >= MINIMUM_DRAG && m_CurrentValue >= 1)
		{
			
			m_CurrentSlideValue = -1;		
			m_IsSliding = true;
			
		}

		else if(Diff < 0  && Diff <= MINIMUM_DRAG &&  m_CurrentValue < m_MaxValue)
		{
			m_CurrentSlideValue = 1;		
			m_IsSliding = true;
			
		}
	}
	

	if(m_IsSliding)
	{
		if(m_SlideDir == SLIDE_DIR_HOR)
			m_ToSlidePos.x = (m_CurrentValue + m_CurrentSlideValue)  * -m_SingleDist;
		else if(m_SlideDir == SLIDE_DIR_VER)
			m_ToSlidePos.y = (m_CurrentValue + m_CurrentSlideValue)  * -m_SingleDist;
	}
	else	
	{
		// snap back or move to neartest item
		const math::Vec2 TempPos = m_pUIMainBox->GetPosition();
		float Dist = 0;

		if(m_SlideDir == SLIDE_DIR_HOR)
		{
			Dist = TempPos.x / m_SingleDist;
			
			int Num = (int)(Dist) * -1;
			float Akywin = (-Dist) - Num;

			if( Akywin >= 0.5f )
				Num++;

			if(Num < 0) 
				Num = 0;

			if( Num > m_MaxValue)
				Num = m_MaxValue ;

			float Target = m_SingleDist * Num;

			if(m_CurrentValue != Num)
			{
				if(m_ChangeCallBack)
					m_ChangeCallBack(ui::UIWidget::WE_SLIDER_CHANGE ,m_pUIMainBox);
			}

			m_CurrentValue = Num;
			m_pUIMainBox->SetPosition( math::Vec2( -Target , TempPos.y ));

			if(m_ChangeCallBack)
				m_ChangeCallBack(ui::UIWidget::WE_SLIDER_CHANGE ,m_pUIMainBox);
		
		}
		else if(m_SlideDir == SLIDE_DIR_VER)
		{
			Dist = TempPos.y / m_SingleDist;
			
			int Num = (int)(Dist) * -1;
			float Akywin = (-Dist) - Num;

			if( Akywin >= 0.5f )
				Num++;

			if(Num < 0) 
				Num = 0;

			if( Num > m_MaxValue)
				Num = m_MaxValue;

			float Target = m_SingleDist * Num;

			if(m_CurrentValue != Num)
			{
				if(m_ChangeCallBack)
					m_ChangeCallBack(ui::UIWidget::WE_SLIDER_CHANGE ,m_pUIMainBox);
			}

			m_CurrentValue = Num;
			m_pUIMainBox->SetPosition( math::Vec2( TempPos.x , -Target));
			

		}

	}

	
}

void	UISlideBox::MouseButtonDown(const math::Vec2& Pos)
{

	if(m_IsSliding)
		return;	

	if(!m_IsMouseDown)
	{
		if(Pos.x < m_TopLeft.x )
			return;

		if(Pos.y < m_TopLeft.y )
			return;

		if(Pos.x > m_RightBottom.x )
			return;

		if(Pos.y > m_RightBottom.y )
			return;

		m_MouseDownStartPos = Pos;
		m_TimeSinceMouseDown = 0;
		m_UILastKnownPos = m_pUIMainBox->GetPosition();
	}
	else
	{
		math::Vec2 TempPos;
		TempPos = m_UILastKnownPos;
		float Diff = 0;

		if(m_SlideDir == SLIDE_DIR_HOR)
		{
			Diff = Pos.x - m_MouseDownStartPos.x;
			TempPos.x += Diff;

			//if(TempPos.x < 0)
			//	TempPos.x = 0;

			//if(TempPos.x > m_SingleDist * m_MaxValue)
			//	TempPos.x = m_SingleDist * m_MaxValue;
		}
		else
		{
			Diff = Pos.y - m_MouseDownStartPos.y;
			TempPos.y += Diff;

			//if(TempPos.y < 0)
			//	TempPos.y = 0;

			//if(TempPos.y > m_SingleDist * m_MaxValue)
			//	TempPos.y = m_SingleDist * m_MaxValue;
		}

		
		
		m_pUIMainBox->SetPosition(TempPos);

	}

	m_IsMouseDown = true;

	


}
